<template>
  <div class="m-2.5 p-2.5 bg-white">
    <div>
      <el-form ref="queryRef" :model="queryParams" label-width="80px">
        <el-form-item label="所属库">
          <el-select v-model="queryParams.dsId" @change="handeDsChange" style="width: 400px">
            <el-option v-for="(ds, index) in dsList" :key="index" :label="ds.name" :value="ds.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="sql语句" style="width: 100%" prop="sql">
          <div class="w-full">
            <CodeEditor v-model:value="queryParams.sql" height="130px" width="100%" />
          </div>
        </el-form-item>
        <el-form-item>
          <div class=""></div>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table v-loading="loading" :data="list">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column
        v-for="(item, index) in tableCols"
        :key="index"
        :label="item.columnLabel + '(' + item.tableName + ')'"
        align="center"
        :prop="item.tableName + '.' + item.columnName"
      />
    </el-table>
    <div>
      <div class="p-3 text-right">
        <el-button
          icon="ArrowLeft"
          :disabled="pageParams.pageNum === 1"
          @click="handlePageChange('Pre')"
        />
        <el-button type="primary" @click="handlePageChange('Now')"
          >{{ pageParams.pageNum }}
        </el-button>
        <el-button icon="ArrowRight" @click="handlePageChange('Next')" />
      </div>
    </div>
    <!-- <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageParams.pageNum"
      v-model:limit="pageParams.pageSize"
      @pagination="getList"
    /> -->
  </div>
</template>
<script setup>
  import { ref } from 'vue';
  import { listSdkDqDs } from '@/api/web/db/dataquery/sdkDqDs';
  import { pageSdkDqSqlQuery } from '@/api/web/db/dataquery/sdkDqQuery';
  import { CodeEditor } from '@/components/CodeEditor';

  const queryParams = ref({
    dsId: null,
    countTotal: false,
    sql: null,
  });
  const queryRef = ref(null);

  const dsList = ref([]);
  const tableCols = ref([]);
  const pageParams = ref({
    pageNum: 1,
    pageSize: 10,
  });
  const total = ref(0);
  const loading = ref(false);
  const list = ref([]);

  // 数据库列表
  async function getdsAll() {
    const res = await listSdkDqDs().catch(() => {});
    console.log({ res });
    dsList.value = res?.data || [];
    return Promise.resolve();
  }

  function handeDsChange() {
    // getList();
  }

  function handlePageChange(type) {
    switch (type) {
      case 'Pre':
        pageParams.value.pageNum = pageParams.value.pageNum - 1;
        break;
      case 'Now':
        break;
      case 'Next':
        pageParams.value.pageNum = pageParams.value.pageNum + 1;
        break;
      default:
        break;
    }
    getList();
  }
  function getList() {
    loading.value = true;
    console.log({ queryParams: queryParams.value });
    pageSdkDqSqlQuery(pageParams.value, queryParams.value)
      .then((res) => {
        console.log({ res });
        const { metas, data } = res.data;
        list.value = data ?? [];
        tableCols.value = metas;
        total.value = res?.data?.total ?? 0;
      })
      .finally(() => {
        loading.value = false;
      });
  }

  function handleQuery() {
    pageParams.value.pageNum = 1;
    getList();
  }

  function resetQuery() {
    queryRef.value?.resetFields();
    handleQuery();
  }

  getdsAll();
  // getList();
</script>
<style lang="scss" scoped></style>
